package org.apache.lucene.demo;

import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;

import java.io.*;

public class TestAnalyzer
{
  public static void main(String[] args) throws Exception
  {
    String string = new String("中华人民共和国");
    TokenStream ts;
    
    System.out.println("IKAnalyzer:");
    Analyzer analyzer = new IKAnalyzer();
    ts = analyzer.tokenStream("dummy", new StringReader(string));
    displayTokenStream(ts);
    
    String str = new String("女儿\\发现\\爸爸\\(有)\\外遇\\(了)\\");
    System.out.println("SimpleAnalyzer:");
    analyzer = new SimpleAnalyzer();
    ts = analyzer.tokenStream("dummy", new StringReader(str));
    displayTokenStream(ts);
  }
  
  private static void displayTokenStream(TokenStream ts) throws IOException
  {
	TermAttribute termAtt = (TermAttribute)ts.getAttribute(TermAttribute.class);
	
	while (ts.incrementToken())
	{
		System.out.print(termAtt.term());
		System.out.print(' ');
	}
	System.out.println(' ');
  }
  
  public static String getContent(String filename) throws Exception
  {
     FileInputStream fis=new FileInputStream(filename);
     InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
     BufferedReader br=new BufferedReader(isr);
     StringBuffer sbContent=new StringBuffer();
     String sLine;
     while((sLine=br.readLine())!=null){
        String s=sLine.toString()+"\n";
        sbContent=sbContent.append(s);
     }
     return sbContent.toString();
  }
}
